<?php
require_once '../include/template_parser.php';
require_once '../include/print_calendario.php';

class CalendarView {
	private $print_calendario;		// Mini calendario

	function __construct() {
		$this->print_calendario = new PrintCalendario();
	}

	function display( $year, $month, $day, $nickname, $ls_calendarios, $ls_event, $ls_tareas, $url_admin = '') {
		$nmonth = $month + 1;
		if( $nmonth == 13) {
			$nmonth = 1;
			$nyear++;
		} else {
			$nyear = $year;
		}

		$ls_cal = $this->print_ls_calendarios( $ls_calendarios);
		$ls_tar = $this->print_ls_tareas( $ls_tareas);
		
		$tags = array(
				'top_bar.php' => '../template/top_bar.php',
				'texto_down1' => 'VSN LCC &#169; 2012',
				'nombre_usuario' => '&nbsp'.$nickname.'&nbsp',
				'lmini_tittle' => $this->print_calendario->generate_tittle( $year, $month),
				'rmini_tittle' => $this->print_calendario->generate_tittle( $nyear, $nmonth),
				'mini_lcal' => $this->print_mini_calendar( $year, $month),
				'mini_rcal' => $this->print_mini_calendar( $year, $month + 1),
				'task_by_day' => $this->print_calendario->generate_week_by_day( $year, $month, $day),
				'task_by_time' => $this->print_calendario->generate_week_by_time( $year, $month, $day, $ls_event),
				'month_names' => $this->print_calendario->get_month_names(),
				'lista_calendarios' => $ls_cal,
				'lista_tareas' => $ls_tar,

				'ano' => $year,
				'mes' => $month,
				'dia' => $day,

				'FUNC_CAL' => FUNC_CAL,
				'FCAL_AJAX' => FCAL_AJAX,
				'CAJAX_PRINT_MCAL' => CAJAX_PRINT_MCAL,
				'CAJAX_PRINT_WEEK_BY_DAY' => CAJAX_PRINT_WEEK_BY_DAY,
				'CAJAX_PRINT_WEEK_BY_TIME' => CAJAX_PRINT_WEEK_BY_TIME,
				'FCAL_ADD_EVENT' => FCAL_ADD_EVENT,
				'FCAL_ADD_CALENDARIO' => FCAL_ADD_CALENDARIO,
				'FADDE_MAIN' => FADDE_MAIN,
				'FADDC_MAIN' => FADDC_MAIN,
				'panel_admin' => $url_admin
			);
		
		$template_parser = new TemplateParser( '../template/calendar_template.php', $tags);
		echo $template_parser->parse_template();
	}

	function print_mini_calendar( $year, $month) {
		date_default_timezone_set('America/Santiago');
		$time = time();
		$this_year = date('Y', $time);
		$this_month = date('n', $time);
		$this_day = date('j',$time);

		if( $this_year == $year && $this_month == $month) {
			$days = array($this_day=>array(NULL,NULL,'<span style="border: solid 1px #aaa; display:block;">'.$this_day.'</span>')); 
		} else {
			$days = array();
		}
		return $this->print_calendario->generate_calendar( $year, $month, $days);
	}

	function print_tittle( $year, $month, $day = 0) {
		return $this->print_calendario->generate_tittle( $year, $month, $day);
	}

	function print_week_by_day( $year, $month, $day) {
		return $this->print_calendario->generate_week_by_day( $year, $month, $day);
	}

	function print_week_by_time( $year, $month, $day, $ls_event) {
		return $this->print_calendario->generate_week_by_time( $year, $month, $day, $ls_event);
	}

	function print_ls_calendarios( $calendarios) {
		if( $calendarios == NULL) return '';
		$response = '';
		foreach( $calendarios as $cal) {
			$response .= sprintf( "<div class=\"div_ls\"><input type='checkbox' name='cal%s' value='%s' checked=\"checked\" />&nbsp;%s</div>", $cal[0], $cal[0], $cal[1] );
		}
		return $response;
	}

	function print_ls_tareas( $tareas) {
		if( $tareas == NULL) return '';
		$response = '';
		foreach( $tareas as $tar) {
			$response .= sprintf( "<div class=\"div_ls\"><input type='checkbox' name='tar%s' value='%s' checked=\"checked\" />&nbsp;%s</div>", $tar['Even_id'], $tar['Even_id'], $tar['Even_titulo'] );
		}
		return $response;
	}
}
?>
